<template>
    <div>
        <div style="display: flex;align-items: center;justify-content: space-between;padding: 1rem .5rem 0;">
            <el-button @click="goback">返回</el-button>
            <div>答题记录</div>
        </div>
        <!--模板列表-->
        <el-card class="box-card" v-for="(v,index) in tableData" :key="index" style="margin-top: 1rem;">
            <div style="display: flex;justify-content: space-between;align-items: center" @click="goDone(v)">
                <div class="text item">
                    <div style="font-size: 0.8rem;">{{v.create_time}}</div>
                </div>
                <div>{{v.score}}</div>
                <!--<div><i class="el-icon-arrow-right"></i></div>-->
                <div v-show="v.is_submit=='0'"><el-button type="primary" @click.stop="goExam(v)">去答题</el-button></div>
            </div>
        </el-card>

        <!--<div style="text-align: center;margin: 2rem 0 0;" v-show="tableData.length>=1">-->
            <!--<el-button type="primary" @click="creatPaper">再次生成试卷</el-button>-->
        <!--</div>-->

        <div class="bottom-btn">
            <el-button-group style="width: 100%;display: flex;">
                <el-button type="default" icon="el-icon-arrow-left" @click="pre">上一页</el-button>
                <el-button type="primary" @click="next">下一页<i class="el-icon-arrow-right el-icon--right"></i></el-button>
            </el-button-group>
        </div>
    </div>
</template>

<script>
    export default {
        name: "examList",
        data(){
            return {
                questionBankId: '',

                tableData: [],
                pageSize: 0,
                totalRow: 0,
                currentPage: 1,
            }
        },
        methods:{
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            // 上一题
            pre(){
                if(this.qSort>0) {
                    this.qSort = this.qSort-1;
                }
            },
            // 下一题
            next(){
                if(this.qSort<(this.tableData.length)) {
                    this.submitQus();
                }
            },
            // 生成试卷
            creatPaper(){
                this.loadopen();
                this.config.appPost('/user/testpaper/createTestPaper', {
                    modeId: this.questionBankId,
                }).then(res => {
                    this.loadclose();
                    if(res.data.code === '0'){
                        this.getData()
                    }else if(res.data.code == '-1003'){
                        this.$message.error(res.data.message);
                    }else {
                        this.$message.error(res.data.message);
                    }
                });
            },
            // 获取试卷列表
            getData(pageNumber) {
                this.loadopen();
                this.config.appPost('/user/testpaper/findUserTestPaperPage', {
                    pageNum: pageNumber?pageNumber:1,
                    modeId: this.questionBankId,
                }).then(res=>{
                    this.loadclose();
                    if(res.data.code==='0'){
                        this.tableData = res.data.page.list;
                        this.pageSize = res.data.page.pageSize;
                        this.totalRow = res.data.page.totalRow;
                        this.pageNumber = res.data.page.pageNumber;

                        if(!res.data.page.list.length){
                            this.creatPaper();
                        }
                    }else {
                        this.$message.error(this.config.handleError(res.data));
                    }
                })
            },
            // 继续答题
            goExam(v){
                this.$router.push({path: '/exam', query: {questionBankId: this.questionBankId, userTestPaperId: v.id}});
            },
            // 查看成绩
            goDone(v){
                if(v.is_submit=='1'){
                    this.$router.push({path: '/examDone', query: {questionBankId: this.questionBankId, userTestPaperId: v.id}})
                }
            },
            goback(){this.$router.back();}
        },
        mounted(){
            let pid = this.$route.query['questionBankId']||this.questionBankId;
            this.questionBankId = pid;
            this.getData();
        }
    }
</script>

<style lang="less" scoped>
    /*上一题 下一题*/
    .bottom-btn{
        position: absolute;
        width: 100%;
        bottom: 0;
        left: 0;
        .el-button{
            width: 50%;
            height: 2.5rem;
        }
    }
</style>
